<?php 
require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex");
MYTCONN();
if(!empty($_GET)) { $_POST = $_GET;}
	$gridLines = ''; ?>
<div style="width:96%; border:1 solid #000;padding:3px;margin:10px">
<table width="100%" border="0" cellpadding="3" cellspacing="1">

<tr bgcolor="#C7C7C7" height="22">
	<td NOWRAP>Data</td>
	<td NOWRAP>Movimento</td>
	<td NOWRAP>COMPRA</td>
	<td NOWRAP width="90">TM COMPRA</td>
	<td NOWRAP width="90">TM COMPRA AC</td>
	<td NOWRAP width="90">VENDA</td>
	<td NOWRAP width="90">TM VENDA</td>
	<td NOWRAP>DIF VENDA/COMPRA AC</td>
	<td NOWRAP>Saldo</td>
	<td NOWRAP>Saldo <?php echo $_SESSION[$SESSST.'CURS'][$_POST['rp_Currency2']]['CurLabel'];?></td>
	<td NOWRAP>Resultado</td>
	<td NOWRAP>Resultado <?php echo $_SESSION[$SESSST.'CURS'][$_POST['rp_Currency2']]['CurLabel'];?></td>
</tr>
<?php
  
	/*	$CallMain = $GLOBALS['MYT']->db_array("SELECT exchange_profit.expenses_ref,
								exchange_profit.ref,
								exchange_profit.TimeProfit,
								exchange_profit.Rate,
								exchange_profit.operations_total,
								(exchange_expenses.tax_value+exchange_expenses.other) AS expenses
		 FROM exchange_profit
		INNER JOIN exchange_expenses ON exchange_expenses.ref = exchange_profit.expenses_ref
		ORDER BY TimeProfit DESC");*/

		$GET = $GLOBALS['MYT']->db_array("SELECT 
					DATE(Time) AS Time, 
					CONCAT('Total ',IF(type_ref=1,'COMPRA','VENDA'),' ',OriNombre) AS Moviment, 
					IF(type_ref=1,SUM(Amount),0) AS SUM_BUY, 
					IF(type_ref=2,SUM(acerto),0) AS SUM_SELL, 
					IF(type_ref=1,SUM(Amount*tax1),0) AS AVG_BUY, 
					IF(type_ref=2,SUM(acerto*tax1),0) AS AVG_SELL,
					type_ref,
					ExcOrigen0
		 FROM exchange_operations
		 INNER JOIN origenes ON OriNumero = ExcOrigen0
			WHERE Status NOT IN (1,5)
				AND Time BETWEEN '{$_POST['rp_DateStart']} 00:00:00' AND '{$_POST['rp_DateEnd']} 23:59:59'
				AND {$_POST['rp_Currency']} IN (in_currency_ref,out_currency_ref)
		GROUP BY DATE(Time),ExcOrigen0,type_ref
	UNION
		SELECT 			CONCAT(DATE(Time),' ',TIME(Time)) AS Time, 
					IF(OperationType=1,'COMPRA FINANCEIRO','VENDA FINANCEIRO') AS Moviment, 
					IF(OperationType=1,SUM(AmountUSED),0) AS SUM_BUY, 
					IF(OperationType=2,SUM(AmountUSED),0) AS SUM_SELL, 
					IF(OperationType=1,SUM(AmountUSED*Rate),0) AS AVG_BUY, 
					IF(OperationType=2,SUM(AmountUSED*Rate),0) AS AVG_SELL,
					OperationType AS type_ref,
					0 AS ExcOrigen0
		 FROM exchange_profit
			WHERE xps_Flag NOT IN (1,5)
				AND TimeProfit BETWEEN '{$_POST['rp_DateStart']} 00:00:00' AND '{$_POST['rp_DateEnd']} 23:59:59'
				AND {$_POST['rp_Currency']} IN (Currency)
		GROUP BY DATE(TimeProfit),OperationType

	UNION 
		SELECT 
					'' AS Time, 
					'Transporte' AS Moviment, 
					IF(type_ref=1,SUM(Amount),0-SUM(acerto)) AS SUM_BUY, 
					0 AS SUM_SELL, 
					IF(type_ref=1,SUM(Amount*tax1),0-SUM(acerto*tax1)) AS AVG_BUY, 
					0 AS AVG_SELL,
					1 AS type_ref,
					0 AS ExcOrigen0
		 FROM exchange_operations
			WHERE Status NOT IN (1,5,6)
				AND Time < '{$_POST['rp_DateStart']} 00:00:00' AND Time > '2009-07-01 00:00:00'
				AND {$_POST['rp_Currency']} IN (in_currency_ref,out_currency_ref)
		ORDER BY Time, ExcOrigen0, Moviment");
	$ctr_sum = 0;
	$Balance = 0;
	$AvgBalance = 0;
	$avg_buy = 0;
	$avg_buyac = 0;
	$sum_profit = 0;
	$sum_buy = 0;
	$sum_sell = 0;
		for ($m = 1; $m <= $GET[0]; $m++) { 
		$Balance += $GET[$m]['SUM_BUY']-$GET[$m]['SUM_SELL'];
		$AvgBalance += $GET[$m]['AVG_BUY']-($GET[$m]['SUM_SELL']*$avg_buyac);
		$avg_sell = 0;
		$avg_selldif = 0;
		$sell_result = 0;
		if ($GET[$m]['AVG_SELL'] > 0) {
		$sum_sell += $GET[$m]['SUM_SELL'];
		$avg_sell = $GET[$m]['AVG_SELL']/$GET[$m]['SUM_SELL'];
		$avg_selldif = $avg_sell-$avg_buyac;
		$sell_result = round($GET[$m]['SUM_SELL']*$avg_selldif,2);
		$sum_profit += $sell_result;
		}
		//$avg_buy = 0;
		//$avg_buyac = 0;
		if ($GET[$m]['AVG_BUY'] > 0) {
		$sum_buy += $GET[$m]['SUM_BUY'];
		$avg_buyac = $AvgBalance/$Balance;
		$avg_buy = $GET[$m]['AVG_BUY']/$GET[$m]['SUM_BUY'];
		}
		if ($GET[$m]['type_ref'] == 1) { $gridline = '#F2F2F2'; } else { $gridline = '#FFFFCC'; }
		$ctr_sum += $sell_result*$avg_buyac;
		if(($_POST['ExcType'] == '' || $_POST['ExcType'] == $GET[$m]['type_ref']) && ($_POST['ExcOrigen0'] == '' || $_POST['ExcOrigen0'] == $GET[$m]['ExcOrigen0'])) {
		$gridLines .= "<tr bgcolor=\"{$gridline}\" height=\"20\">
				<td NOWRAP>".$GET[$m]['Time']."</td>
				<td NOWRAP>".str_replace('MONEYONE EXPRESS','',$GET[$m]['Moviment'])."</td>
				<td NOWRAP align=\"right\">".number_format($GET[$m]['SUM_BUY'],2).' '.$_SESSION[$SESSST.'CURS'][$_POST['rp_Currency']]['CurLabel']."</td>
				<td NOWRAP align=\"right\">".number_format($avg_buy,5)."</td>
				<td NOWRAP align=\"right\">".number_format($avg_buyac,5)."</td>
				<td NOWRAP align=\"right\">".number_format($GET[$m]['SUM_SELL'],2).' '.$_SESSION[$SESSST.'CURS'][$_POST['rp_Currency']]['CurLabel']."</td>
				<td NOWRAP align=\"right\">".number_format($avg_sell,5)."</td>
				<td NOWRAP align=\"right\">".number_format($avg_selldif,5)."</td>
				<td NOWRAP align=\"right\">".number_format($Balance,2).' '.$_SESSION[$SESSST.'CURS'][$_POST['rp_Currency']]['CurLabel']."</td>
				<td NOWRAP align=\"right\">".number_format($Balance*$avg_buyac,2).' '.$_SESSION[$SESSST.'CURS'][$_POST['rp_Currency2']]['CurLabel']."</td>
			    <td NOWRAP align=\"right\">".number_format($sell_result,2).' '.$_SESSION[$SESSST.'CURS'][$_POST['rp_Currency']]['CurLabel']."</td>
				<td NOWRAP align=\"right\">".number_format($sell_result*$avg_buyac,2).' '.$_SESSION[$SESSST.'CURS'][$_POST['rp_Currency2']]['CurLabel']."</td>
			</tr>";
		}
			}
		
		$gridLines .= "<tr><td colspan=\"12\" style=\"background-color:#000\"></td></tr>
				<tr height=\"20\">
				<td NOWRAP><b>Total Geral: </b></td>
				<td NOWRAP></td>
				<td NOWRAP align=\"right\"><b>".number_format($sum_buy,2).' '.$_SESSION[$SESSST.'CURS'][$_POST['rp_Currency']]['CurLabel']."</b></td>
				<td NOWRAP align=\"right\"><b>".number_format($avg_buy,5)."</b></td>
				<td NOWRAP align=\"right\"><b>".number_format($avg_buyac,5)."</b></td>
				<td NOWRAP align=\"right\"><b>".number_format($sum_sell,2).' '.$_SESSION[$SESSST.'CURS'][$_POST['rp_Currency']]['CurLabel']."</b></td>
				<td NOWRAP align=\"right\"><b>".number_format($avg_sell,5)."</b></td>
				<td NOWRAP align=\"right\"><b>".number_format($avg_selldif,5)."</b></td>
				<td NOWRAP align=\"right\"><b>".number_format($Balance,2).' '.$_SESSION[$SESSST.'CURS'][$_POST['rp_Currency']]['CurLabel']."</b></td>
				<td NOWRAP align=\"right\"><b>".number_format($Balance*$avg_buyac,2).' '.$_SESSION[$SESSST.'CURS'][$_POST['rp_Currency2']]['CurLabel']."</b></td>
			    <td NOWRAP align=\"right\"><b>".number_format($sum_profit,2).' '.$_SESSION[$SESSST.'CURS'][$_POST['rp_Currency']]['CurLabel']."</b></td>
				<td NOWRAP align=\"right\"><b>".number_format($ctr_sum,2).' '.$_SESSION[$SESSST.'CURS'][$_POST['rp_Currency2']]['CurLabel']."</b></td>
			</tr>";
	echo $gridLines;
?>
</table>
</div>